<!--
 * @Author: hq
 * @Date: 2022-05-28 19:29:52
 * @LastEditors: hq
 * @LastEditTime: 2022-07-22 11:35:26
 * @Description: 添加代金券
 * @version: 1.0
-->
<template>
  <div class="change">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="套餐名" prop="name">
            <el-input
              v-model="ruleForm.name"
              placeholder="请输入套餐名"
            ></el-input>
          </el-form-item>
          <el-form-item label="缩略图" :required="true">
            <el-upload
              v-loading="loading"
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :http-request="upLoadServe"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="抵扣金额" prop="oprice">
            <el-input
              maxlength="10"
              v-model="ruleForm.oprice"
              placeholder="请输入抵扣金额"
            ></el-input>
          </el-form-item>
          <el-form-item label="购买金额" prop="price">
            <el-input
              maxlength="10"
              v-model="ruleForm.price"
              placeholder="请输入购买金额"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存" prop="stock">
            <el-input
              maxlength="10"
              v-model="ruleForm.stock"
              placeholder="请输入库存"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="使用范围" :required="true">
        <el-row>
          <el-col :span="9">
            <el-time-select
              placeholder="起始时间"
              v-model="startTime"
              :picker-options="{
                start: '08:30',
                step: '00:15',
                end: '18:30',
              }"
            >
            </el-time-select
          ></el-col>

          <el-col :span="9"
            ><el-time-select
              placeholder="结束时间"
              v-model="endTime"
              :picker-options="{
                start: '08:30',
                step: '00:15',
                end: '18:30',
                minTime: startTime,
              }"
            >
            </el-time-select
          ></el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="状态" :required="true">
        <el-switch
          active-text="禁用"
          inactive-text="启用"
          :active-value="1"
          :inactive-value="0"
          class="tablescope"
          v-model="ruleForm.status"
        >
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { editCashCoupon, createCashCoupon } from "@/api/shopSystem";
import upImg from "@/utils/upimg";
export default {
  props: ["rowData"],
  data() {
    return {
      loading: false,
      rowId: "",
      ruleForm: {
        name: "",
        oprice: "",
        price: "",
        stock: "",
        status: 1,
      },
      startTime: "",
      endTime: "",
      imageUrl: "",
      rules: {
        name: [{ required: true, message: "请输入套餐名", trigger: "blur" }],
        oprice: [
          { required: true, message: "请输入抵扣金额", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入购买金额", trigger: "blur" }],
        stock: [{ required: true, message: "请输入库存", trigger: "blur" }],
      },
    };
  },
  methods: {
    async createCashCoupon() {
      const { msg, err } = await createCashCoupon({
        name: this.ruleForm.name,
        oprice: this.ruleForm.oprice,
        price: this.ruleForm.price,
        stock: this.ruleForm.stock,
        thumb: this.imageUrl,
        status: this.ruleForm.status,
        use_range: this.startTime + " - " + this.endTime,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功!",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    async editCashCoupon() {
      const { err, msg } = await editCashCoupon({
        cm_id: this.rowData.id,
        name: this.ruleForm.name,
        oprice: this.ruleForm.oprice,
        price: this.ruleForm.price,
        stock: this.ruleForm.stock,
        thumb: this.imageUrl,
        status: this.ruleForm.status,
        use_range: this.startTime + " - " + this.endTime,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功!",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    upLoadServe(file) {
      this.loading = true;
      upImg.upimg(
        (data) => {
          this.imageUrl = data.data.url;
          this.loading = false;
        },
        1,
        file
      );
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //接口
          if (this.rowData.id) {
            //编辑
            this.editCashCoupon();
          } else {
            this.createCashCoupon();
          }
        }
      });
    },
  },
  created() {
    if (this.rowData.id) {
      console.log(this.rowData);
      //编辑
      this.ruleForm.name = this.rowData.name;
      this.ruleForm.oprice = this.rowData.oprice;
      this.ruleForm.price = this.rowData.price;
      this.ruleForm.stock = this.rowData.stock;
      this.ruleForm.status = this.rowData.status;
      this.imageUrl = this.rowData.thumb;
      this.startTime = this.rowData.use_range.split(" - ")[0];
      this.endTime = this.rowData.use_range.split(" - ")[1];
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader {
  width: 82px;
  height: 82px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
